package com.yeung;

import com.yeung.bean.Department;
import com.yeung.bean.Employee;
import com.yeung.mapper.DepartmentMapper;
import com.yeung.mapper.EmployeeDynamicSqlMapper;
import com.yeung.mapper.EmployeeMapper;
import com.yeung.mapper.EmployeeMapperPlus;
import com.yeung.mapper.annotation.EmployeeMapperAnnotation;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;


/**
 * mybatis-config.xml是全局配置文件,包裹数据库连接池信息和事务等等
 * mapper映射文件 (必须要有),映射sql
 */
public class 动态sql测试 {
    private static SqlSessionFactory sessionFactory;
    private static SqlSession session;

    static {
        InputStream resourceAsStream = null;
        try {
            //拿到配置文件的数据流
            resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            System.out.println("找不到这个配置文件");
        }
        //创建 sessionFactory
        sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
    }

    public static SqlSession getSession() {
        if (session == null)
            session = sessionFactory.openSession();
        return session;
    }

    /**
     * 多条件查询
     * 一次使用多个条件
     */
    @Test
    public void test() {
        //创建session对象 操作sql
        SqlSession session = getSession();
        //第一种调用方法
        EmployeeDynamicSqlMapper mapper = session.getMapper(EmployeeDynamicSqlMapper.class);
        Employee emp = new Employee(4, "yeung", null, "男");
        List<Employee> e = mapper.getEmpsByConditionIf(emp);
        System.out.println(e.get(0));

    }

    /**
     * 多条件查询
     * 一次使用一个条件
     */
    @Test
    public void test2() {
        //创建session对象 操作sql
        SqlSession session = getSession();
        //第一种调用方法
        EmployeeDynamicSqlMapper mapper = session.getMapper(EmployeeDynamicSqlMapper.class);
        Employee emp = new Employee(4, "yeung", null, "男");
        List<Employee> e = mapper.getEmpsByConditionIf2(emp);
        System.out.println(e.get(0));

    }

    //修改
    @Test
    public void test3() {
        //创建session对象 操作sql
        SqlSession session = getSession();
        //第一种调用方法
        EmployeeDynamicSqlMapper mapper = session.getMapper(EmployeeDynamicSqlMapper.class);
        Employee emp = new Employee(4, "yeungChan", null, "女");
        mapper.updateEmp(emp);
    }

    /**
     * 批量查询
     */
    @Test
    public void test4() {
        //创建session对象 操作sql
        SqlSession session = getSession();
        //第一种调用方法
        EmployeeDynamicSqlMapper mapper = session.getMapper(EmployeeDynamicSqlMapper.class);
        List<Employee> emps = mapper.getEmpsByConditionForeach(Arrays.asList(1, 2, 3, 4));
        System.out.println(emps.size());
    }

    /**
     * 批量保存
     */
    @Test
    public void test5() {
        //创建session对象 操作sql
        SqlSession session = getSession();
        //第一种调用方法
        EmployeeDynamicSqlMapper mapper = session.getMapper(EmployeeDynamicSqlMapper.class);
        List<Employee> emps = new ArrayList<>();
        Department department = new Department(2, null, null);
        Employee e1 = new Employee(null, "smith", "dds@gmail.com", "男");
        e1.setDepartment(department);
        emps.add(e1);
        Employee e2 = new Employee(null, "allen", "allen@qq.com", "女");
        e2.setDepartment(department);
        emps.add(e2);
        mapper.addEmps2(emps);

    }

    @After
    public void close() {
        if (session != null)
            session.close();
    }
}
